package com.example.myapplication22;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.myapplication22.utils.MyHelper;

public class Login extends AppCompatActivity {
    EditText u,p;
    ImageView img;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
       u = findViewById(R.id.userId);
       p=findViewById(R.id.passWord);
       img=findViewById(R.id.iv2);//头像

        Bundle bundle = getIntent().getExtras();
        //当存放数据不为空时
        SharedPreferences data = getSharedPreferences("data", MODE_PRIVATE);

        if (data.getString("userId","")!=null){
            u.setText(data.getString("userId",""));
            SharedPreferences.Editor edit = data.edit();
            edit.remove("userId");
            edit.commit();
        }
        Button login = findViewById(R.id.login);
        final   RadioButton zh = findViewById(R.id.zh);
        Button register1 = findViewById(R.id.register);
        //注册的点击事件
        register1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Login.this,Register.class);
                startActivity(intent);
            }
        });

        //login点击事件
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (zh.isChecked()){//记住账号
                    SharedPreferences data = getSharedPreferences("data", MODE_PRIVATE);
                    SharedPreferences.Editor edit = data.edit();
                    edit.putString("userId",String.valueOf(u.getText()));
                    edit.commit();
                }
                //验证账号密码
                SQLiteDatabase value = new MyHelper(Login.this).getWritableDatabase();

                Cursor cursor = value.rawQuery("select * from user where userId=? and password=?",
                        new String[]{u.getText().toString(),p.getText().toString()});

                if (cursor!=null && cursor.moveToNext() ){
                    Toast.makeText(Login.this,"success",Toast.LENGTH_SHORT).show();
                    //跳转
                    Intent intent = new Intent(Login.this, List.class);
                    Bundle b = new Bundle();
                    //存放用户_id
                    b.putString("id",cursor.getString(1));
                    b.putString("img",cursor.getString(3));
                    //带值跳转
                    intent.putExtras(b);
                    startActivity(intent);
                    System.out.println("登陆成功");
                    return;
                }else {
                    Toast.makeText(Login.this,"error",Toast.LENGTH_SHORT).show();
                    return;
                }


            }
        });
        if (bundle == null) {//没有数据时

            Button register = findViewById(R.id.register);
            //启动自动检索头像线程
            new Thread(new AutoCheckImg()).start();
            //注册的点击事件
            register.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(Login.this,Register.class);
                    startActivity(intent);
                }
            });
            RadioGroup rg = findViewById(R.id.rg);
            //单选按钮事件
            rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, int i) {


                }
            });
            return;

        } else {//注册带了数据回来
            ImageView iv2 = findViewById(R.id.iv2);
            if (bundle.getString("tag") == null) {//注册按键

                int iv = bundle.getInt("iv");
                String userId = bundle.getString("userId");
                String password = bundle.getString("password");

                iv2.setImageResource(iv);
               u.setText(userId);
               p.setText(password);
                Toast.makeText(Login.this, iv, Toast.LENGTH_SHORT).show();


                //当存放数据不为空时
                /*SharedPreferences data = getSharedPreferences("data", MODE_PRIVATE);

                if (data.getString("userId","")!=null){
                    u.setText(data.getString("userId",""));

                    SharedPreferences.Editor edit = data.edit();
                    edit.remove("userId");
                    edit.commit();
                }*/
            } else {//返回按键
                // iv2.setImageResource(R.drawable.t02);
            }
        }
    }

    //自动检测头像
    class AutoCheckImg implements Runnable{
        @Override
        public void run() {
            SQLiteDatabase w = new MyHelper(Login.this).getWritableDatabase();
            while (true){
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Cursor cursor = w.rawQuery("select img from user where userId=?", new String[]{u.getText().toString()});
                if (cursor.getCount()!=0){
                    cursor.moveToFirst();
                    int i = cursor.getInt(0);
                    img.setImageResource(i);

                }

            }
        }
    }


}
